<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, user-scalable=no">
  <meta name="description" content="API docs for the downloadMessage method from the TIMMessageManager class, for the Dart programming language.">
  <title>downloadMessage method - TIMMessageManager class - tim_message_manager library - Dart API</title>


  
  <link rel="preconnect" href="https://fonts.gstatic.com">
  <link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@0,300;0,400;0,500;0,700;1,400&display=swap" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0" rel="stylesheet">
  
  <link rel="stylesheet" href="../../static-assets/github.css?v1">
  <link rel="stylesheet" href="../../static-assets/styles.css?v1">
  <link rel="icon" href="../../static-assets/favicon.png?v1">

  
</head>


<body data-base-href="../../" data-using-base-href="false" class="light-theme">

<div id="overlay-under-drawer"></div>

<header id="title">
  <span id="sidenav-left-toggle" class="material-symbols-outlined" role="button" tabindex="0">menu</span>
  <ol class="breadcrumbs gt-separated dark hidden-xs">
    <li><a href="../../index.html">tencent_cloud_chat_sdk</a></li>
    <li><a href="../../native_im_adapter_tim_message_manager/native_im_adapter_tim_message_manager-library.html">native_im&#47;adapter&#47;tim_message_manager.dart</a></li>
    <li><a href="../../native_im_adapter_tim_message_manager/TIMMessageManager-class.html">TIMMessageManager</a></li>
    <li class="self-crumb">downloadMessage method</li>
  </ol>
  <div class="self-name">downloadMessage</div>
  <form class="search navbar-right" role="search">
    <input type="text" id="search-box" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
  </form>
  <div class="toggle" id="theme-button" title="Toggle brightness">
    <label for="theme">
      <input type="checkbox" id="theme" value="light-theme">
      <span id="dark-theme-button" class="material-symbols-outlined">
        dark_mode
      </span>
      <span id="light-theme-button" class="material-symbols-outlined">
        light_mode
      </span>
    </label>
  </div>
</header>
<main>

<div
    id="dartdoc-main-content"
    class="main-content"
    data-above-sidebar="native_im_adapter_tim_message_manager&#47;TIMMessageManager-class-sidebar.html"
    data-below-sidebar="">
    <div>
<h1><span class="kind-method">downloadMessage</span> method 
</h1></div>

    <section class="multi-line-signature">
      

<span class="returntype"><a href="https://api.flutter.dev/flutter/dart-async/Future-class.html">Future</a><span class="signature">&lt;<wbr><span class="type-parameter"><a href="../../models_v2_tim_callback/V2TimCallback-class.html">V2TimCallback</a></span>&gt;</span></span>
<span class="name ">downloadMessage</span>(<wbr><ol class="parameter-list"><li><span class="parameter" id="downloadMessage-param-msgID">{<span>required</span> <span class="type-annotation"><a href="https://api.flutter.dev/flutter/dart-core/String-class.html">String</a></span> <span class="parameter-name">msgID</span>, </span></li>
<li><span class="parameter" id="downloadMessage-param-imageType"><span>required</span> <span class="type-annotation"><a href="https://api.flutter.dev/flutter/dart-core/int-class.html">int</a></span> <span class="parameter-name">imageType</span>, </span></li>
<li><span class="parameter" id="downloadMessage-param-isSnapshot"><span>required</span> <span class="type-annotation"><a href="https://api.flutter.dev/flutter/dart-core/bool-class.html">bool</a></span> <span class="parameter-name">isSnapshot</span>, </span></li>
<li><span class="parameter" id="downloadMessage-param-downloadPath"><span class="type-annotation"><a href="https://api.flutter.dev/flutter/dart-core/String-class.html">String</a>?</span> <span class="parameter-name">downloadPath</span>}</span></li>
</ol>)

      

    </section>
    


    
<section class="summary source-code" id="source">
  <h2><span>Implementation</span></h2>
  <pre class="language-dart"><code class="language-dart">Future&lt;V2TimCallback&gt; downloadMessage({
  required String msgID,
  required int imageType,
  required bool isSnapshot,
  String? downloadPath,
}) async {
  if (!TIMManager.instance.isInitSDK()) {
    return V2TimCallback(code: TIMErrCode.ERR_SDK_NOT_INITIALIZED.value, desc: &quot;sdk not init&quot;);
  }

  final downloadKey = &#39;msgID-$imageType-$isSnapshot&#39;;
  if (_downloadingMessageSet.contains(downloadKey)) {
    print(&quot;message ID: $msgID, imageType: $imageType, isSnapshot: $isSnapshot is downloading&quot;);
    return V2TimCallback(code: TIMErrCode.ERR_INVALID_PARAMETERS.value, desc: &#39;message is downloading&#39;);
  }

  V2TimValueCallback&lt;List&lt;V2TimMessage&gt;&gt; findResult = await findMessages(messageIDList: [msgID]);
  if (findResult.code != TIMErrCode.ERR_SUCC.value) {
    print(&quot;downloadMessage, find message failed&quot;);
    return V2TimCallback(code: findResult.code, desc: findResult.desc);
  }

  List&lt;V2TimMessage&gt; msgList = findResult.data!;
  if (msgList.isEmpty) {
    print(&quot;downloadMessage, message not found&quot;);
    return V2TimCallback(code: TIMErrCode.ERR_INVALID_PARAMETERS.value, desc: &quot;message not found&quot;);
  }

  V2TimMessage message = msgList[0];
  if (!{MessageElemType.V2TIM_ELEM_TYPE_IMAGE, MessageElemType.V2TIM_ELEM_TYPE_VIDEO, MessageElemType.V2TIM_ELEM_TYPE_SOUND, MessageElemType.V2TIM_ELEM_TYPE_FILE}.contains(message.elemType)) {
    print(&quot;downloadMessage, message does not support downloading&quot;);
    return V2TimCallback(code: TIMErrCode.ERR_INVALID_PARAMETERS.value, desc: &quot;message does not support downloading&quot;);
  }

  V2TimMessageDownloadElemParam downloadParam = V2TimMessageDownloadElemParam(message: message, imageType: imageType, isSnapshot: isSnapshot);
  if (downloadParam.downloadUrl.isEmpty || downloadParam.fileUUID.isEmpty) {
    print(&quot;downloadMessage, message missing necessary download info&quot;);
    return V2TimCallback(code: TIMErrCode.ERR_INVALID_PARAMETERS.value, desc: &quot;message missing necessary download info&quot;);
  }
  String adjustDownloadPath = downloadPath ?? _getDefaultCachePath(message: message, imageType: imageType, isSnapshot: isSnapshot, downloadParam: downloadParam);

  if (File(adjustDownloadPath).existsSync()) {
    return V2TimCallback(code: TIMErrCode.ERR_SUCC.value, desc: &quot;file: $adjustDownloadPath already exists!&quot;);
  }

  String userData = Tools.generateUserData(&#39;downloadMessage&#39;);
  Completer&lt;V2TimCallback&gt; completer = Completer();
  void handleApiCallback(Map jsonResult) {
    V2TimValueCallback&lt;dynamic&gt; result = V2TimValueCallback&lt;dynamic&gt;.fromJson(jsonResult);
    if (result.desc == &#39;downloading&#39;) {
      Map&lt;String, dynamic&gt; processInfo = result.data ?? {};
      if (processInfo.isNotEmpty) {
        int currentSize = processInfo[&#39;msg_download_elem_result_current_size&#39;] ?? 0;
        int totalSize = processInfo[&#39;msg_download_elem_result_total_size&#39;] ?? 0;

        V2TimMessageDownloadProgress downloadProgress = V2TimMessageDownloadProgress(
          isFinish: currentSize &gt; 0 &amp;&amp; currentSize == totalSize,
          isError: result.code == TIMErrCode.ERR_SUCC.value,
          msgID: msgID,
          currentSize: currentSize,
          totalSize: totalSize,
          type: imageType,
          isSnapshot: isSnapshot,
          path: adjustDownloadPath,
          errorCode: result.code,
          errorDesc: result.desc,
        );

        &#47;&#47; 下载进度回调
        _advancedMessageListener.onMessageDownloadProgressCallback(downloadProgress);
      }
    } else {
      &#47;&#47; 下载完成回调
      _downloadingMessageSet.remove(downloadKey);

      completer.complete(V2TimCallback(code: result.code, desc: result.desc));
    }
  }
  NativeLibraryManager.timApiValueCallback2Future(userData, handleApiCallback);

  _downloadingMessageSet.add(downloadKey);

  Pointer&lt;Char&gt; pDownloadParam = Tools.string2PointerChar(json.encode(downloadParam.toJson()));
  Pointer&lt;Char&gt; pDownloadPath = Tools.string2PointerChar(adjustDownloadPath);
  Pointer&lt;Void&gt; pUserData = Tools.string2PointerVoid(userData);
  NativeLibraryManager.bindings.DartDownloadElemToPath(pDownloadParam, pDownloadPath, pUserData);

  return completer.future;
}</code></pre>
</section>


  </div> <!-- /.main-content -->

  <div id="dartdoc-sidebar-left" class="sidebar sidebar-offcanvas-left">
    <!-- The search input and breadcrumbs below are only responsively visible at low resolutions. -->
<header id="header-search-sidebar" class="hidden-l">
  <form class="search-sidebar" role="search">
    <input type="text" id="search-sidebar" autocomplete="off" disabled class="form-control typeahead" placeholder="Loading search...">
  </form>
</header>

<ol class="breadcrumbs gt-separated dark hidden-l" id="sidebar-nav">
    <li><a href="../../index.html">tencent_cloud_chat_sdk</a></li>
    <li><a href="../../native_im_adapter_tim_message_manager/native_im_adapter_tim_message_manager-library.html">tim_message_manager</a></li>
    <li><a href="../../native_im_adapter_tim_message_manager/TIMMessageManager-class.html">TIMMessageManager</a></li>
    <li class="self-crumb">downloadMessage method</li>
</ol>


    <h5>TIMMessageManager class</h5>
    <div id="dartdoc-sidebar-left-content"></div>
  </div><!--/.sidebar-offcanvas-->

  <div id="dartdoc-sidebar-right" class="sidebar sidebar-offcanvas-right">
</div><!--/.sidebar-offcanvas-->

</main>

<footer>
  <span class="no-break">
    tencent_cloud_chat_sdk
      8.5.6864-beta.7
  </span>

  
</footer>



<script src="../../static-assets/highlight.pack.js?v1"></script>
<script src="../../static-assets/docs.dart.js"></script>



</body>

</html>

